<template>
	<view class='main-container'>
		<!--头部 搜索框开始-->
		<view class='search'>
			<view class='inp'>
				<image src='../../static/images/search1.png'></image>
				<input placeholder="搜索问题" auto-focus="true" v-model="searchValue" maxlength="20" />
				<image src='../../static/images/close1.png' v-if="searchValue.length!=0" @tap='clear'></image>
			</view>
			<text @tap='search'>搜索</text>
		</view>
		<view class="tuijian" v-if="isDis">
			<view class="title">
				热门搜索
			</view>
			<view class="detail">
				<text @tap="doSearch('成绩')">怎么查成绩?</text>
				<text @tap="doSearch('毕业论文')">毕业论文怎么写?</text>
				<text @tap="doSearch('坐车')">双福怎么坐车到重庆西站?</text>
				<text @tap="doSearch('表白')">遇到喜欢的人需要表白吗?</text>
			</view>
		</view>
		<view class="tuijian" v-if="isDis&&historys.length!=0">
			<view class="title">
				<text>历史搜索</text>
				<text style="color: #586C94;font-size: 28upx;font-weight: 500;" @tap="clearHistory">清空历史</text>
			</view>
			<view class="detail" v-for="(item,index) in historys" :key="index">
				<text @tap="doSearch(item)">{{item}}</text>
			</view>
		</view>
		<view class="question" v-for="(item,index) in questions" :key="index" @tap="go_detail(item.id)" v-if="!isDis">
			<view class="top">
				{{item.questionTitle}}
			</view>
			<view class="mid1" v-if="item.questionImgUrl.length!=0">
				<text>{{item.questionDesc}}</text>
				<image :src="getRealPath(item.questionImgUrl)"></image>
			</view>
			<view class="mid2" v-if="item.questionImgUrl.length==0">
				<text>{{item.questionDesc}}</text>
			</view>
			<view class="bottom">
				<view class="left">
					{{item.stars}} 赞同 · {{item.answers}} 回答
				</view>
				<image src="../../static/images/sandian.png" mode="" @tap="more"></image>
			</view>
		</view>
	</view>
</template>

<script>
	import utils from "../../static/js/utils.js";
	export default {
		onLoad() {
			let historys = uni.getStorageSync("questionhis") || [];
			this.historys = historys;
		},
		data() {
			return {
				questions: [],
				searchValue: '',
				historys: []
			}
		},
		computed: {
			isDis() {
				if (this.searchValue.length == 0) {
					return true;
				}
				return false;
			}
		},
		methods: {
			formatTime(timestamp) {
				var da = new Date(timestamp);
				return (da.getMonth() + 1) + "-" + da.getDate() + "  " + da.getHours() + ":" + da.getMinutes();
			},
			getRealPath(path) {
				return utils.baseImgUrl + path;
			},
			search() {
				this.doSearch(this.searchValue);
			},
			clear() {
				this.searchValue = '';
			},
			go_detail(index) {
				uni.navigateTo({
					url: 'questiondetail?questionId=' + index
				})
			},
			doSearch(value) {
				if (value.length == 0) {
					return;
				}
				this.searchValue = value;
				// utils.loading("搜索中...");
				let that = this;
				this.historys.push(value);
				let set = new Set(this.historys);
				this.historys = [...set];
				uni.setStorageSync("questionhis", this.historys);
				utils.post("question/search", {
					searchValue: value
				}, function(data) {
					that.questions = data;
				})
			},
			clearHistory() {
				this.historys = [];
				uni.removeStorageSync("questionhis");
			}
		}

	}
</script>

<style>
	page {
		background-color: #f6f6f6;
	}

	.main-container {
		display: flex;
		width: 100%;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.search {
		width: 100%;
		height: 100upx;
		background-color: #ffffff;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
	}

	.inp {
		width: 650upx;
		height: 70upx;
		display: flex;
		flex-direction: row;
		background-color: white;
		align-items: center;
		margin-left: 20upx;
		border-radius: 8upx;
		background-color: #f6f6f6;
		font-size: 26upx;
	}

	.inp image {
		width: 48upx;
		height: 48upx;
	}

	.inp input {
		width: 554upx;
	}

	.search text {
		font-size: 28upx;
		margin-left: 10upx;
		color: #666666;
	}

	.tuijian {
		width: 100%;
		margin-top: 30upx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.title {
		width: 92%;
		height: 80upx;
		font-size: 32upx;
		font-weight: 600;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.detail {
		width: 100%;
		display: flex;
		flex-direction: column;
		background-color: #ffffff;
		align-items: center;
	}

	.detail text {
		width: 92%;
		font-size: 28upx;
		height: 100upx;
		line-height: 100upx;
		box-shadow: inset 0px -1px 1px -1px #dcdcdc;
	}

	.question {
		width: 100%;
		margin-top: 20upx;
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: #ffffff;
	}

	.top {
		width: 92%;
		height: 70upx;
		font-size: 30upx;
		font-weight: 600;
		line-height: 80upx;
	}

	.mid1 {
		width: 92%;
		height: 160upx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	.mid1 text {
		max-width: 400upx;
		font-size: 24upx;
		height: 94upx;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
		overflow: hidden;
	}

	.mid1 image {
		height: 160upx;
		width: 260upx;
		border-radius: 20upx;
	}

	.mid2 {
		width: 92%;
		height: 120upx;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.mid2 text {
		font-size: 24upx;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
		overflow: hidden;
		height: 94upx;
	}

	.bottom {
		width: 92%;
		height: 70upx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.left {
		font-size: 22upx;
		color: #999999;
		height: 80upx;
		line-height: 80upx;

	}

	.bottom image {
		width: 40upx;
		height: 40upx;
	}
</style>
